﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Values
{
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Core.Validators;

	public class UserMessageFolderUpdate : ValueBase<UserMessageFolderUpdate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserMessageFolderUpdate"/> class.
		/// </summary>
		public UserMessageFolderUpdate()
		{
			
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UserMessageFolderUpdate"/> class.
		/// </summary>
		/// <param name="folder">The folder.</param>
		public UserMessageFolderUpdate(UserMessageFolder folder) 
			: this()
		{
			this.Id = folder.Id;
			this.Title = folder.Title;
			this.TitleBefore = folder.Title;
		}

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public int Id
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title before.
		/// </summary>
		/// <value>The title before.</value>
		public string TitleBefore
		{
			get;
			set;
		}

		/// <summary>
		/// Gets a value indicating whether [title changed].
		/// </summary>
		/// <value><c>true</c> if [title changed]; otherwise, <c>false</c>.</value>
		public bool TitleChanged
		{
			get
			{
				return (this.Title != this.TitleBefore);
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserMessageFolderUpdate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			if (this.TitleChanged)
			{
				rules.Add(x => x.Title)
					.Custom(new UserMessageFolderUniqueTitleValidator(), Resources.Errors.UserMessageFolderTitleTaken);
			}

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}